package com.fxf.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxf.srb.core.enums.TransTypeEnum;
import com.fxf.srb.core.mapper.UserInfoMapper;
import com.fxf.srb.core.pojo.bo.TransFlowBO;
import com.fxf.srb.core.pojo.entity.TransFlow;
import com.fxf.srb.core.mapper.TransFlowMapper;
import com.fxf.srb.core.pojo.entity.UserInfo;
import com.fxf.srb.core.service.ITransFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author fxf
 * @since 2021-05-26
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements ITransFlowService {

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 增加交易流水
     * @param transFlowBO
     */
    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {

        //获取用户信息
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bind_code",transFlowBO.getBindCode());
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        TransFlow transFlow = new TransFlow();
        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransNo(transFlowBO.getAgentBillNo());
        transFlow.setTransType(transFlowBO.getTransTypeEnum().getTransType());
        transFlow.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());
        transFlow.setMemo(transFlowBO.getMemo());


        baseMapper.insert(transFlow);


    }

    /**
     * 判断交易流水是否已经添加，保证接口幂等性
     * @param agentBillNo
     * @return
     */
    @Override
    public boolean isSaveTransFlow(String agentBillNo) {
        QueryWrapper<TransFlow> wrapper = new QueryWrapper<>();
        wrapper.eq("trans_no",agentBillNo);
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer > 0){
            return true;
        }
        return false;

    }

    @Override
    public List<TransFlow> selectByUserId(Long userId) {
        QueryWrapper<TransFlow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<TransFlow> transFlowsList = baseMapper.selectList(wrapper);

        return transFlowsList;
    }
}
